From 0172c7181e465c4af5309fcf0eec557edf6ba6ed Mon Sep 17 00:00:00 2001 From: "emellor@leeni.uk.xensource.com" Date: Fri, 2 Dec 2005 01:04:09 +0000 Subject: [PATCH] Use getopt.gnu_getopt rather than getopt.getopt, so that xm list VM --long is parsed the same as xm list --long VM. This is only in Python 2.3 -- for Python 2.2 we keep the old getopt.getopt behaviour. Signed-off-by: Ewan Mellor --- tools/python/xen/xm/main.py | 18 +++++++++++++----- tools/python/xen/xm/opts.py | 10 ++++++---- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/tools/python/xen/xm/main.py b/tools/python/xen/xm/main.py index bd4d03da47..83c1bc4794 100644 --- a/tools/python/xen/xm/main.py +++ b/tools/python/xen/xm/main.py @@ -25,7 +25,7 @@ import os import os.path import sys import re -from getopt import getopt +import getopt import socket import warnings warnings.filterwarnings('ignore', category=FutureWarning) @@ -39,6 +39,14 @@ from xen.xm.opts import * import console + +# getopt.gnu_getopt is better, but only exists in Python 2.3+. Use +# getopt.getopt if gnu_getopt is not available. This will mean that options +# may only be specified before positional arguments. +if not hasattr(getopt, 'gnu_getopt'): + getopt.gnu_getopt = getopt.getopt + + # Strings for shorthelp console_help = "console Attach to domain DomId's console." create_help = """create [-c] @@ -332,8 +340,8 @@ def xm_list(args): use_long = 0 show_vcpus = 0 try: - (options, params) = getopt(args, 'lv', ['long','vcpus']) - except GetoptError, opterr: + (options, params) = getopt.gnu_getopt(args, 'lv', ['long','vcpus']) + except getopt.GetoptError, opterr: err(opterr) sys.exit(1) @@ -729,8 +737,8 @@ def xm_network_detach(args): def xm_vnet_list(args): from xen.xend.XendClient import server try: - (options, params) = getopt(args, 'l', ['long']) - except GetoptError, opterr: + (options, params) = getopt.gnu_getopt(args, 'l', ['long']) + except getopt.GetoptError, opterr: err(opterr) sys.exit(1) diff --git a/tools/python/xen/xm/opts.py b/tools/python/xen/xm/opts.py index 3f983407fc..e75af798ae 100644 --- a/tools/python/xen/xm/opts.py +++ b/tools/python/xen/xm/opts.py @@ -13,11 +13,12 @@ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #============================================================================ # Copyright (C) 2004, 2005 Mike Wray +# Copyright (C) 2005 XenSource Ltd. #============================================================================ """Object-oriented command-line option support. """ -from getopt import getopt, GetoptError +import getopt import os import os.path import sys @@ -333,9 +334,10 @@ class Opts: while args: # let getopt parse whatever it feels like -- if anything try: - (xvals, args) = getopt(args[0:], - self.short_opts(), self.long_opts()) - except GetoptError, err: + (xvals, args) = getopt.getopt(args[0:], + self.short_opts(), + self.long_opts()) + except getopt.GetoptError, err: self.err(str(err)) for (k, v) in xvals: -- 2.30.2